﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using QQ.Util;
using System.Threading;
using RuoTeng.Util;
using System.Windows.Forms;
using System.Net;
using AmazonProductAdvtApi;
using System.Xml;
using RTCRMQQ1.lib;

namespace RTGather.Lib.gather
{

    /// <summary>
    ///亞馬遜網站採集
    ///
    /// </summary>
    public class fbb_bugamazon
    {
         
        private string countpattern = @"(?isx)16\sof\s(?<content>.*?)\sResults</span>";//总条数

        private string asinpattern = @"(?isx)dp/(?<content>.*?)/ref";//asin 

        private string areaPattern = @"(?isx)<div\sclass=""title"">(?<content>.*?)<span\sclass=""ptBrand""";//块

        private string titlePattern = @"(?isx)<a.*?"">(?<content>.*?)</a>";//标题  

        private string descurlPattern = @"(?isx)href=""(?<content>.*?)"">";//描述 

        private string descPattern = @"(?isx)class=\u0022productDescriptionWrapper\u0022>(?<desc>.*?)</div>";//描述

        
        private static string rootpath = Application.StartupPath;//根目录 

        private string oldgather = rootpath + "\\已采集产品.txt";

        private CookieContainer cc = new CookieContainer();

  
        private List<Product> clist = new List<Product>();
        public  fbb_bugamazon()
        {
          
        }

        /// <summary>
        /// 生成请求连接
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public string getUrl(string keyword,int page)
        { 
            string url = string.Format("http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords={0}&page={1}", keyword, page);
            return url; 
        }

        public void getData(string url,string code,string format)
        {
            string html = Http.GetData(url, null, cc, Encoding.UTF8);

            while (true)
            {
                if (html.Contains("<title>500 Service Unavailable Error</title>")||html=="")
                {
                    html = Http.GetData(url, null, cc, Encoding.UTF8);
                    Thread.Sleep(1000);
                }
                else
                {
                    break;
                }
            }

            MatchCollection matches = Regex.Matches(html, areaPattern);
            clist.Clear();

            foreach (Match match in matches)
            {
                string innerurl = "";
                GroupCollection groups = match.Groups;

                if (groups.Count > 0)
                {

                    innerurl = groups["content"].ToString();
                    string pname = getStrFromPattern(titlePattern, innerurl, "content");
                    string pid = getStrFromPattern(asinpattern, innerurl, "content");
                     
                    string descurl = getStrFromPattern(descurlPattern, innerurl, "content");

                    bool succ = descurl.StartsWith("http://");


                    Product p = new Product();

                    if (IsRepeat(pid, oldgather) == false&&succ==true)
                    {
                        p.Name = pname;
                        p.Pid = pid;
                        p.vcode =makeCode(code, pid);

                        string innerurl2 = descurl;
                        string innerhtml = Http.GetData(descurl, null, cc, Encoding.UTF8);

                        while (true)
                        {
                            if (innerhtml.Contains("<title>500 Service Unavailable Error</title>")||innerhtml=="")
                            {
                                innerhtml = Http.GetData(descurl, null, cc, Encoding.UTF8);
                                Thread.Sleep(1000);
                            }
                            else
                            {
                                break;
                            }


                        }

                        string desccrption = getStrFromPattern(descPattern, innerhtml, "desc");
                        desccrption = NoHTML(desccrption);

                        p.desc = pname + "<br>" + desccrption;
                        if (desccrption == "没有登记")
                        {
                            p.desc = pname + "<br>";
                        }

                        //结束
                        //写入文件
                        writeNew(pid, oldgather);
                        clist.Add(p);
                    }
                }

            }
        }


        /// <summary>
        /// 判断是否重复
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        private bool IsRepeat(string url, string filename)
        {
            List<string> list = TextBoxTools.getFileList(filename);
            bool isrepeat = list.Contains(url);
            return isrepeat;
        } 

        /// <summary>
        /// 写入新的记录
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="filename"></param>
        private void writeNew(string pid, string filename)
        {
            string old = RTFile.ReadFile(filename);
            string newstring = old + pid + System.Environment.NewLine;
            RTFile.WriteFile(filename, newstring); 
        }
         
        public string getStrFromPattern(string pattern,string content,string name)
        {
            Match match = Regex.Match(content, pattern);
            string result="";
            if (match.Success)
            {
                GroupCollection groups = match.Groups;
                result = groups[name].ToString();
            }
            else
            {
                result = "没有登记";
            }
            return result; 
        }

        public string makeCode(string code, string pid)
        {
            string result = string.Format("<a href=\"http://www.amazon.com/gp/product/{1}/ref=as_li_qf_br_asin_il_tl?ie=UTF8&tag={0}&linkCode=as2&camp=1789&creative=9325&creativeASIN={1}\">Learn more</a>", code, pid);
            return result;
        }

        /// <summary>
        /// 根据关键词搜索提取条数
        /// </summary>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public int getCount(string keyword)
        {
            string url = string.Format("http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords={0}&x=19&y=14", keyword);
            string tem = Http.GetData("http://www.amazon.com/", cc);

            while (true)
            {
                if (tem.Contains("<title>500 Service Unavailable Error</title>")||tem=="")
                { 
                    tem = Http.GetData("http://www.amazon.com/", cc);
                    Thread.Sleep(1000);
                }
                else
                {
                    break;
                }
            }
            string result = Http.GetData(url, cc);

            while (true)
            {
                if (result.Contains("<title>500 Service Unavailable Error</title>")||result=="")
                {
                    result = Http.GetData(url, cc);
                    Thread.Sleep(1000);
                }
                else
                {
                    break;
                }
            }

            string countstr = getStrFromPattern(countpattern, result, "content").Replace(",", "");

            int count = ParseType.ParseInt(countstr, 0);

            return count;

        }
         

        /// <summary>
        /// 计算页数 
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        public int getPageCount(int count)
        { 
            int PageSize = 16; 
            int pagenum = (count / PageSize) + 1; 
            return pagenum;
        }
 
 
        public string NoHTML(string Htmlstring)
        {
            //删除脚本
            Htmlstring = Htmlstring.Replace("\r\n", "");
            Htmlstring = Regex.Replace(Htmlstring, @"<script.*?</script>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<style.*?</style>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<.*?>", "", RegexOptions.IgnoreCase);
            //删除HTML
            Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"<!--.*", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", "", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
            Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
            Htmlstring = Htmlstring.Replace("<", "");
            Htmlstring = Htmlstring.Replace(">", "");
            Htmlstring = Htmlstring.Replace("\r\n", "");
            return Htmlstring;
        }




        /// <summary>
        /// 导出 文件excel
        /// </summary>
        /// <param name="filename"></param>
        public void ExcelExport(string filename)
        {
            Excel.Application xls_app = null;
            Excel._Workbook xls_book = null;
            Excel._Worksheet xls_sheet = null;
            System.Reflection.Missing miss = System.Reflection.Missing.Value;
            try
            {
                xls_app = new Excel.ApplicationClass();
                xls_app.Visible = false;
                Excel.Workbooks workBooks = (Excel.Workbooks)xls_app.Workbooks;
                if (!System.IO.File.Exists(filename))
                {
                    xls_book = (Excel.Workbook)workBooks.Add(true);
                    Excel.Sheets _sheets = xls_book.Sheets;
                    xls_sheet = (Excel.Worksheet)_sheets[1];
                    int columnIndex = 1;

                    xls_sheet.Cells[1, columnIndex] = "产品编号"; columnIndex++; 
                    xls_sheet.Cells[1, columnIndex] = "产品标题"; columnIndex++; 
                   xls_sheet.Cells[1, columnIndex] = "产品描述"; columnIndex++;  
                    xls_sheet.Cells[1, columnIndex] = "产品代码"; columnIndex++; 
                   
                   
                    xls_book.SaveAs(filename, miss, miss, miss, miss, miss, Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss, miss, miss);
                    xls_book.Close(false, miss, miss);

                }
                xls_book = workBooks.Open(filename, miss, miss, miss, miss, miss, Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss, miss, miss);
                Excel.Sheets sheets = xls_book.Sheets;
                xls_sheet = (Excel.Worksheet)sheets[1];

                int rowIndex = xls_sheet.UsedRange.Rows.Count == 0 ? 1 : xls_sheet.UsedRange.Rows.Count + 1;

                foreach (Product c in this.clist)
                {

                    int columnIndex = 1;

                    xls_sheet.Cells[rowIndex, columnIndex] = c.Pid;
                    columnIndex++;


                    xls_sheet.Cells[rowIndex, columnIndex] = c.Name;
                    columnIndex++;

                    xls_sheet.Cells[rowIndex, columnIndex] = c.desc;
                    columnIndex++;

                    xls_sheet.Cells[rowIndex, columnIndex] = c.vcode;
                    columnIndex++;

                    rowIndex++;

                }
                xls_book.Save();
                xls_book.Close(false, miss, miss);
                xls_app.Quit();
            }
            catch(Exception ex) {
            	ex.ToString();            	if (xls_book != null) { xls_book.Save(); xls_book.Close(false, miss, miss); xls_app.Quit(); } }
        }


    }
}
